<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/layuimini.css?v=2.0.4.2}" media="all">
    <link rel="stylesheet" th:href="@{/css/themes/default.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <style>
        body {
            padding: 20px;
        }
    </style>
</head>
<body>

<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">员工姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" required  lay-verify="required" placeholder="请输入员工姓名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="M" title="男">
            <input type="radio" name="sex" value="F" title="女" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-block">
            <input type="text" name="age" required  lay-verify="required|number" placeholder="请输入年龄" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">工资</label>
        <div class="layui-input-block">
            <input type="text" name="sal" required  lay-verify="required|number" placeholder="请输入工资" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">生日</label>
        <div class="layui-input-block">
            <input type="text" name="birthday" required  lay-verify="required" placeholder="请输入生日" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">部门</label>
        <div class="layui-input-block">
            <select name="deptId" lay-verify="required">
                <option value="">请选择</option>
                <option th:each="dept:${deptList}" th:value="${dept.deptId}">
                    <span th:text="${dept.deptName}"></span>
                </option>
            </select>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">家庭地址</label>
        <div class="layui-input-block">
            <textarea name="address" placeholder="请输入家庭地址" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button id="btn-submit" class="layui-btn" lay-submit lay-filter="btn-submit-filter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script th:src="@{/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js?v=2.0.0}" charset="utf-8"></script>
<script th:inline="javascript">
    layui.use(["laydate","table","form"],function(){

        var laydate = layui.laydate;// 中文日期
        var table = layui.table // 表格
        var form = layui.form; // 表单
        var $ = layui.$ // 获取到jQuery对象


        laydate.render({
            elem: '[name="birthday"]'
        });


        form.on('submit(btn-submit-filter)', function(data){
            $("#btn-submit").attr("disabled", true).addClass("layui-btn-disabled")
            console.log(data.field) // 当前容器的全部表单字段，名值对形式：{name: value}
            var url = "/emp"
            $.post(url,data.field,function (response) {
                if (response.code === 0) {
                    // 成功
                    layer.msg(response.msg, {
                        icon: 1,
                        time: 2000 // 设置 2 秒后自动关闭
                    }, function(){
                        // 调用父窗口的方法
                        parent.refreshTable()
                        // 关闭当前frame
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    });
                } else {
                    // 移除按钮禁用特效
                    $("#btn-submit").removeAttr("disabled", true).removeClass("layui-btn-disabled")
                    // 失败
                    layer.msg("添加信息失败，请联系管理员", {
                        icon: 2,
                        anim: 6
                    }, function(){
                        // do something
                    });
                }

            })

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


    });
</script>
</body>
</html>